<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:yui="http://yuilibrary.com/rdf/1.0/yui.rdf#">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<title>API: utils   datasource.js  (YUI Library)</title>

	<link rel="stylesheet" type="text/css" href="assets/reset-fonts-grids-min.css?stamp=1316139452.1" />
	<link rel="stylesheet" type="text/css" href="assets/api.css?stamp=1316139452.1" />

    <script type="text/javascript" src="assets/api-js?stamp=1316139452.1"></script>
    <script type="text/javascript" src="assets/ac-js?stamp=1316139452.1"></script>
</head>

<body id="yahoo-com">

<div id="doc3" class="yui-t2">
	<div id="hd">
        <h1><a href="http://developer.yahoo.com/yui/" title="g-javascript-lib">g-javascript-lib</a></h1>
        <h3>utils&nbsp; <span class="subtitle">1.0.0</span></h3>
        <a href="./index.html" title="g-javascript-lib">g-javascript-lib</a> 
            &gt; <a href="./module_utils.html" title="utils">utils</a>
                
                 &gt; datasource.js (source view) 
        <form onsubmit="return false">
            <div id="propertysearch">
                Search: <input autocomplete="off" id="searchinput" />
                <div id="searchresults">
                    &nbsp;
                </div>
            </div>
        </form>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">
            <form action="#" name="yui-classopts-form" method="get" id="yui-classopts-form">
                <fieldset>
                    <legend>Filters</legend>
                <span class="classopts"><input type="checkbox" name="show_private" id="show_private" /> <label for="show_private">Show Private</label></span>
                <span class="classopts"><input type="checkbox" name="show_protected" id="show_protected" /> <label for="show_protected">Show Protected</label></span>
                <span class="classopts"><input type="checkbox" name="show_deprecated" id="show_deprecated" /> <label for="show_deprecated">Show Deprecated</label></span>
                </fieldset>
            </form>

                    <div id="srcout">
                        <style>
                            #doc3 .classopts { display:none; }
                        </style>
<div class="highlight"><pre><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">$</span><span class="p">){</span>

	<span class="cm">/**</span>
<span class="cm">	 * &lt;P&gt; DataSource is a collection of data, that user can get part or all of them, </span>
<span class="cm">	 * and step in them to get every piece of it.&lt;/P&gt;</span>
<span class="cm">	 * </span>
<span class="cm">	 * &lt;P&gt;The data can come from Array or Ajax. DataSource just define an interface, </span>
<span class="cm">	 * the class of DataSourceArray and DataSourceAjax will do the real things.&lt;/P&gt;</span>
<span class="cm">	 *</span>
<span class="cm">	 * &lt;P&gt; DataSource can be connected with a widget to do a paged view of the data</span>
<span class="cm">	 * &lt;/P&gt;</span>
<span class="cm">	 *</span>
<span class="cm">	 * @namespace widget</span>
<span class="cm">	 * @class DataSource</span>
<span class="cm">	 *</span>
<span class="cm">	 */</span>
	<span class="nx">DataSource</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
	<span class="p">}</span>

	<span class="cm">/**</span>
<span class="cm">	 * DataSourceArray is a DataSource based on an array.</span>
<span class="cm">	 * @class DataSourceArray</span>
<span class="cm">	 * @param {Array} arr The real source of data</span>
<span class="cm">	 */</span>
	<span class="nx">DataSourceArray</span> <span class="o">=</span> <span class="nx">G</span><span class="p">.</span><span class="nx">GClass</span><span class="p">(</span><span class="s1">&#39;Widget&#39;</span><span class="p">,</span> <span class="s1">&#39;DataSourceArray&#39;</span><span class="p">,</span> <span class="nx">G</span><span class="p">.</span><span class="nx">Base</span><span class="p">.</span><span class="nx">EventListener</span><span class="p">);</span>
	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__init</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">{</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">__Base__EventListener__init</span><span class="p">();</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">data</span> <span class="o">=</span> <span class="nx">arr</span><span class="p">;</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">pageStart</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">current</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>	<span class="c1">// Current record in page.</span>
	<span class="p">}</span>

	<span class="cm">/**</span>
<span class="cm">	 * Get a page of data. this function will fire the &#39;ondataready&#39; event.</span>
<span class="cm">	 *</span>
<span class="cm">	 * @method get_data</span>
<span class="cm">	 * @param offset {integer} from where the page begin</span>
<span class="cm">	 * @param len {integer} the size of page, the lenght of data returned will </span>
<span class="cm">	 * not greater than the page size.</span>
<span class="cm">	 *</span>
<span class="cm">	 */</span>
	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">get_data</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">offset</span><span class="p">,</span> <span class="nx">len</span><span class="p">)</span> 
	<span class="p">{</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">pageStart</span> <span class="o">=</span> <span class="nx">offset</span><span class="p">;</span>
		<span class="kd">var</span> <span class="nx">i</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="nx">offset</span><span class="p">;</span>
		<span class="k">if</span> <span class="p">(</span><span class="nx">i</span> <span class="o">&gt;</span> <span class="nx">len</span><span class="p">)</span>	<span class="nx">i</span> <span class="o">=</span> <span class="nx">len</span><span class="p">;</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">pageLen</span> <span class="o">=</span> <span class="nx">i</span><span class="p">;</span>

		<span class="c1">// Reset pointer to start of the page.</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">current</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>	

		<span class="k">this</span><span class="p">.</span><span class="nx">fire</span><span class="p">(</span><span class="s2">&quot;ondataready&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;offset&#39;</span><span class="o">:</span><span class="nx">offset</span><span class="p">,</span> <span class="s1">&#39;len&#39;</span><span class="o">:</span> <span class="nx">i</span><span class="p">});</span>
	<span class="p">}</span>

	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">get_page_len</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">pageLen</span><span class="p">;</span>
	<span class="p">}</span>


	<span class="cm">/**</span>
<span class="cm">	 * Get data length in total. This function will fire &quot;ontotalchanged&quot; event.</span>
<span class="cm">	 *</span>
<span class="cm">	 * @mehod get_total</span>
<span class="cm">	 */</span>
	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">get_total</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="k">this</span><span class="p">.</span><span class="nx">fire</span><span class="p">(</span><span class="s2">&quot;ontotalchanged&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">.</span><span class="nx">length</span><span class="p">);</span>
	<span class="p">}</span>
	<span class="cm">/**</span>
<span class="cm">	 * Get next piece of data in current page.</span>
<span class="cm">	 *</span>
<span class="cm">	 * @method step_in_page</span>
<span class="cm">	 * @return {} the data or null if no more data in current page</span>
<span class="cm">	 */</span>
	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">step_in_page</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> 
	<span class="p">{</span>
		<span class="kd">var</span> <span class="nx">ret</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
		<span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">current</span> <span class="o">&lt;</span> <span class="k">this</span><span class="p">.</span><span class="nx">pageLen</span><span class="p">)</span> <span class="p">{</span>
			<span class="nx">ret</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">current</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">pageStart</span><span class="p">];</span>
			<span class="k">this</span><span class="p">.</span><span class="nx">current</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
		<span class="p">}</span>
		<span class="k">return</span> <span class="nx">ret</span><span class="p">;</span>
	<span class="p">}</span>

	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">seek_in_page</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">offset</span><span class="p">)</span>
	<span class="p">{</span>
		<span class="k">if</span> <span class="p">(</span><span class="nx">offset</span> <span class="o">&lt;</span> <span class="k">this</span><span class="p">.</span><span class="nx">pageLen</span><span class="p">)</span> <span class="p">{</span>
			<span class="k">this</span><span class="p">.</span><span class="nx">current</span> <span class="o">=</span> <span class="nx">offset</span><span class="p">;</span>
		<span class="p">}</span>
	<span class="p">}</span>


	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">read</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">offset</span><span class="p">)</span> 
	<span class="p">{</span>
		<span class="k">if</span> <span class="p">(</span><span class="nx">offset</span> <span class="o">&lt;</span> <span class="k">this</span><span class="p">.</span><span class="nx">pageLen</span><span class="p">)</span> <span class="p">{</span>
			<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">pageStart</span> <span class="o">+</span> <span class="nx">offset</span><span class="p">];</span>
		<span class="p">}</span>
		<span class="k">return</span> <span class="kc">null</span><span class="p">;</span>
	<span class="p">}</span>

	<span class="nx">DataSourceArray</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">total</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span>
	<span class="p">{</span>
		<span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">data</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span>
	<span class="p">}</span>

<span class="p">})();</span>
</pre></div>
                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div id="moduleList" class="module">
                        <h4>Modules</h4>
                        <ul class="content">
                                <li class=""><a href="module_base.html" title="base">base</a></li>
                                <li class=""><a href="module_svg.html" title="svg">svg</a></li>
                                <li class=""><a href="module_test.html" title="test">test</a></li>
                                <li class="selected"><a href="module_utils.html" title="utils">utils</a></li>
                                <li class=""><a href="module_widget.html" title="widget">widget</a></li>
                        </ul>
                    </div>

                    <div id="classList" class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="Test.Array.html" title="Test.Array">Test.Array</a></li>
                                <li class=""><a href="Test.List.html" title="Test.List">Test.List</a></li>
                                <li class=""><a href="Test.ListNode.html" title="Test.ListNode">Test.ListNode</a></li>
                                <li class=""><a href="Utils.TreeNode.html" title="Utils.TreeNode">Utils.TreeNode</a></li>
                                <li class=""><a href="widget.DataSource.html" title="widget.DataSource">widget.DataSource</a></li>
                                <li class=""><a href="widget.DataSourceArray.html" title="widget.DataSourceArray">widget.DataSourceArray</a></li>
                        </ul>
                    </div>

                    <div id="fileList" class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class=""><a href="barview.js.html" title="barview.js">barview.js</a></li>
                                <li class="selected"><a href="datasource.js.html" title="datasource.js">datasource.js</a></li>
                                <li class=""><a href="dgrid.js.html" title="dgrid.js">dgrid.js</a></li>
                                <li class=""><a href="list.js.html" title="list.js">list.js</a></li>
                                <li class=""><a href="tree.js.html" title="tree.js">tree.js</a></li>
                                <li class=""><a href="xml.js.html" title="xml.js">xml.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2011 Yahoo! Inc. All rights reserved.
	</div>
</div>
<script type="text/javascript">
    ALL_YUI_PROPS = [{"url": "Test.List.html#method_append", "access": "", "host": "Test.List", "type": "method", "name": "append"}, {"url": "Utils.TreeNode.html#method_append_child", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "append_child"}, {"url": "Test.List.html#method_clear", "access": "", "host": "Test.List", "type": "method", "name": "clear"}, {"url": "Utils.TreeNode.html#method_disconnect", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "disconnect"}, {"url": "Utils.TreeNode.html#method_dump", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "dump"}, {"url": "Utils.TreeNode.html#method_get_child_count", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "get_child_count"}, {"url": "widget.DataSourceArray.html#method_get_data", "access": "", "host": "widget.DataSourceArray", "type": "method", "name": "get_data"}, {"url": "Utils.TreeNode.html#method_get_deep", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "get_deep"}, {"url": "Utils.TreeNode.html#method_get_father", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "get_father"}, {"url": "Utils.TreeNode.html#method_get_fullname", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "get_fullname"}, {"url": "widget.DataSourceArray.html#method_get_total", "access": "", "host": "widget.DataSourceArray", "type": "method", "name": "get_total"}, {"url": "Test.List.html#method_has_only_one", "access": "", "host": "Test.List", "type": "method", "name": "has_only_one"}, {"url": "Utils.TreeNode.html#method___init", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "__init"}, {"url": "Test.List.html#method_insert", "access": "", "host": "Test.List", "type": "method", "name": "insert"}, {"url": "Test.Array.html#method_insertAt", "access": "", "host": "Test.Array", "type": "method", "name": "insertAt"}, {"url": "Test.List.html#method_is_empty", "access": "", "host": "Test.List", "type": "method", "name": "is_empty"}, {"url": "Utils.TreeNode.html#method_move", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "move"}, {"url": "Test.List.html#method_remove", "access": "", "host": "Test.List", "type": "method", "name": "remove"}, {"url": "Test.Array.html#method_removeAt", "access": "", "host": "Test.Array", "type": "method", "name": "removeAt"}, {"url": "Utils.TreeNode.html#method_remove_child", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "remove_child"}, {"url": "Test.List.html#method_search", "access": "", "host": "Test.List", "type": "method", "name": "search"}, {"url": "Utils.TreeNode.html#method_search", "access": "", "host": "Utils.TreeNode", "type": "method", "name": "search"}, {"url": "widget.DataSourceArray.html#method_step_in_page", "access": "", "host": "widget.DataSourceArray", "type": "method", "name": "step_in_page"}];
</script>
</body>
</html>
